package cn.swpu.blog.vo;

import cn.swpu.blog.enums.ResponseEnum;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;

/**
 * @Author 冰翔
 * @Date Created in 7:51 下午 2020/12/10
 */
@Data
@JsonInclude(value = JsonInclude.Include.NON_NULL)
public class ResponseVo<T> {

    private Integer status;

    private String msg;

    private T data;

    public ResponseVo(Integer status, String msg) {
        this.status = status;
        this.msg = msg;
    }

    public ResponseVo(Integer status, String msg, T data) {
        this.status = status;
        this.msg = msg;
        this.data = data;
    }

    public ResponseVo(Integer status, T data) {
        this.status = status;
        this.data = data;
    }

    /**
     * 表示请求成功
     * @return
     */
    public static <T>ResponseVo<T> success(){
        return new ResponseVo<>(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getDesc());
    }

    /**
     * 返回数据data
     */
    public static <T>ResponseVo<T> success(T data){
        return new ResponseVo<>(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getDesc(), data);
    }

    /**
     * 请求失败返回服务端错误
     */
    public static <T>ResponseVo<T> error(){
        return new ResponseVo<>(ResponseEnum.ERROR.getCode(), ResponseEnum.ERROR.getDesc());
    }

    public static <T>ResponseVo<T> error(String msg){
        return new ResponseVo<>(ResponseEnum.ERROR.getCode(), msg);
    }

    public static  <T>ResponseVo<T> error(ResponseEnum responseEnum){
        return new ResponseVo<T>(responseEnum.getCode(), responseEnum.getDesc());
    }

    public static  <T>ResponseVo<T> error(ResponseEnum responseEnum, String msg){
        return new ResponseVo<T>(responseEnum.getCode(), responseEnum.getDesc()+" "+msg);
    }

}
